/**
 * 
 */
package GUI;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileWriter;


public class EventsInPeriod extends JPanel{

	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JLabel iconlabel;
	private JButton btnGenerateReport;
	private JComboBox<String> ToYcomboBox;
	private JComboBox<String> FromYcomboBox;
	private JComboBox<String> ToMcomboBox;
	private JComboBox<String> FromMcomboBox;
	private JComboBox<String> TypeOfStatisticcomboBox;
	
	private String [] monthList;
	private Object syncObject;
	private boolean f_isChangeAllowed;
	private boolean toChangeAllowed;
	/**
	 * Constructor
	 */
	public EventsInPeriod() {
		setLayout(null);
		
		syncObject = new Object();
		btnBack = new JButton("Back");
		monthList =new String[]{"January", "February","March","April","May","June","July","August","September","October","November","December"};
		btnBack.setIcon(new ImageIcon(EventsInPeriod.class.getResource("/resources/back22.png")));
		btnGenerateReport = new JButton("Generate Report");
		
		btnGenerateReport.setIcon(new ImageIcon(EventsInPeriod.class.getResource("/resources/stats.png")));
		ToYcomboBox = new JComboBox<String>();
		ToYcomboBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if(isChangeAllowed()){
				synchronized (syncObject) {
					
					if(Integer.parseInt(ToYcomboBox.getSelectedItem().toString())>Integer.parseInt(FromYcomboBox.getSelectedItem().toString()))
						filterList(ToMcomboBox,ToYcomboBox.getSelectedItem().toString(),"TYEAR");
					else{
						System.out.println("in else ");
						filterList(ToMcomboBox,FromMcomboBox.getSelectedItem().toString(),"FMONTH");
					}
				}
				}
			}
			
		});
		ToYcomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		FromYcomboBox = new JComboBox<String>();
		FromYcomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					synchronized (syncObject) {
					if(isChangeAllowed())
							
							filterList(ToYcomboBox,FromYcomboBox.getSelectedItem().toString(),"YEAR");
					}
			}
		});
		FromYcomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		ToMcomboBox = new JComboBox<String>();
		ToMcomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		ToMcomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		FromMcomboBox = new JComboBox<String>();
		FromMcomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					filterList(ToMcomboBox,FromMcomboBox.getSelectedItem().toString(),"FMONTH");
				}
			}
		});
		FromMcomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		TypeOfStatisticcomboBox = new JComboBox<String>();
		TypeOfStatisticcomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		
		initialize();
	}

	private void initialize() {
		
		JLabel lblStatistics = new JLabel("Statistics");
		lblStatistics.setForeground(new Color(0, 204, 255));
		lblStatistics.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblStatistics.setBounds(10, 11, 101, 25);
		add(lblStatistics);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		JLabel lblFrom = new JLabel("From Month");
		lblFrom.setForeground(Color.WHITE);
		lblFrom.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFrom.setBounds(157, 163, 86, 27);
		add(lblFrom);
		
		JLabel lblTo = new JLabel("To Month");
		lblTo.setForeground(Color.WHITE);
		lblTo.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblTo.setBounds(157, 249, 86, 27);
		add(lblTo);
		
		JLabel lblYear = new JLabel("In The Year");
		lblYear.setForeground(Color.WHITE);
		lblYear.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblYear.setBounds(400, 163, 80, 27);
		add(lblYear);
		
		
		btnGenerateReport.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnGenerateReport.setBounds(316, 327, 182, 44);
		add(btnGenerateReport);
		
		
		JLabel lblToYear = new JLabel("In The Year");
		lblToYear.setForeground(Color.WHITE);
		lblToYear.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblToYear.setBounds(400, 249, 80, 27);
		add(lblToYear);
		
		
		FromMcomboBox.setBounds(253, 167, 104, 27);
		FromMcomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"January", "February","March","April","May","June","July","August","September","October","November","December"}));
		AutoCompletion.enable(this.FromMcomboBox);
		add(FromMcomboBox);
		
		
		ToMcomboBox.setBounds(253, 253, 104, 27);
		//ToMcomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"January", "February","March","April","May","June","July","August","September","October","November","December"}));	
		AutoCompletion.enable(this.ToMcomboBox);
		add(ToMcomboBox);
		
		
		FromYcomboBox.setBounds(490, 167, 114, 27);
		FromYcomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"2014", "2015","2016","2017","2018","2019","2020","2021","2022","2023","2024","2025"}));	
		AutoCompletion.enable(this.FromYcomboBox);
		add(FromYcomboBox);
		
		ToYcomboBox.setBounds(490, 253, 114, 27);
		//ToYcomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"2014", "2015","2016","2017","2018","2019","2020","2021","2022","2023","2024","2025"}));
		AutoCompletion.enable(this.ToYcomboBox);
		add(ToYcomboBox);
		
		
		TypeOfStatisticcomboBox.setBounds(316, 90, 182, 39);
		TypeOfStatisticcomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"Event In Period","Event In Halls","Upcoming Year Events","Meetings Statistics"}));
		add(TypeOfStatisticcomboBox);
		
		JLabel lblStatisticType = new JLabel("Statistic Type");
		lblStatisticType.setForeground(Color.WHITE);
		lblStatisticType.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblStatisticType.setBounds(202, 96, 104, 27);
		add(lblStatisticType);
		
		iconlabel = new JLabel("");
		iconlabel.setForeground(Color.WHITE);
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}

	private void filterList(JComboBox<String> combo, String selectedItem, String scope)
	{
		f_isChangeAllowed=false;
		synchronized (syncObject) {
			combo.removeAllItems();
			
			switch(scope)
			{
			case "YEAR":
				Integer year = Integer.parseInt(selectedItem);
				for (int i=0; i<10;i++) 
				{
					combo.addItem(String.valueOf(year));
					year++;
				}
				break;
			case "TYEAR":
				Integer toYear = Integer.parseInt(selectedItem);
				System.out.println(toYear);
				System.out.println(Integer.parseInt(FromYcomboBox.getSelectedItem().toString()));
				for(int i=0;i<monthList.length;i++)
					combo.addItem(monthList[i]);
				
				break;
			case "FMONTH":
				int i=0;
				System.out.println("in fmonth ");
				System.out.println("selected: "+ selectedItem);
				for (String month :monthList) 
				{
					if(!(month.equals(selectedItem)))
					{
						System.out.println(i);
						i++;
					}
					else
						break;
				}
				for(int j=i;j<monthList.length;j++)
					combo.addItem(monthList[j]);
				break;
			
			}
			
		}
		f_isChangeAllowed=true;
	}
	
	/**
	* @return the toChangeAllowed
	* 
	*/
	private boolean isToChangeAllowed()
	{
		return toChangeAllowed;
	}
	/**
	* @return the f_isChangeAllowed
	* 
	*/
	private boolean isChangeAllowed()
	{
		return f_isChangeAllowed;
	}
	/**
	 * @return the typeOfStatisticcomboBox
	 */
	public JComboBox<String> getTypeOfStatisticcomboBox() {
		return TypeOfStatisticcomboBox;
	}

	/**
	 * @param typeOfStatisticcomboBox the typeOfStatisticcomboBox to set
	 */
	public void setTypeOfStatisticcomboBox(JComboBox<String> typeOfStatisticcomboBox) {
		TypeOfStatisticcomboBox = typeOfStatisticcomboBox;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @return the btnGenerateReport
	 */
	public JButton getBtnGenerateReport() {
		return btnGenerateReport;
	}

	/**
	 * @param btnGenerateReport the btnGenerateReport to set
	 */
	public void setBtnGenerateReport(JButton btnGenerateReport) {
		this.btnGenerateReport = btnGenerateReport;
	}

	/**
	 * @return the toYcomboBox
	 */
	public JComboBox<String> getToYcomboBox() {
		return ToYcomboBox;
	}

	/**
	 * @param toYcomboBox the toYcomboBox to set
	 */
	public void setToYcomboBox(JComboBox<String> toYcomboBox) {
		ToYcomboBox = toYcomboBox;
	}

	/**
	 * @return the fromYcomboBox
	 */
	public JComboBox<String> getFromYcomboBox() {
		return FromYcomboBox;
	}

	/**
	 * @param fromYcomboBox the fromYcomboBox to set
	 */
	public void setFromYcomboBox(JComboBox<String> fromYcomboBox) {
		FromYcomboBox = fromYcomboBox;
	}

	/**
	 * @return the toMcomboBox
	 */
	public JComboBox<String> getToMcomboBox() {
		return ToMcomboBox;
	}

	/**
	 * @param toMcomboBox the toMcomboBox to set
	 */
	public void setToMcomboBox(JComboBox<String> toMcomboBox) {
		ToMcomboBox = toMcomboBox;
	}

	/**
	 * @return the fromMcomboBox
	 */
	public JComboBox<String> getFromMcomboBox() {
		return FromMcomboBox;
	}

	/**
	 * @param fromMcomboBox the fromMcomboBox to set
	 */
	public void setFromMcomboBox(JComboBox<String> fromMcomboBox) {
		FromMcomboBox = fromMcomboBox;
	}
}